<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author: 冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace common\models\system;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "sys_menu".
 *
 * @property int $id 菜单ID
 * @property string $name 菜单名称
 * @property int $parent_id 父菜单ID
 * @property string|null $type 菜单类型（M目录 C菜单 F按钮）
 * @property string|null $perms 权限标识
 * @property string|null $icon 菜单图标
 * @property string|null $url 路由地址
 * @property string|null $query 路由参数
 * @property string|null $component 组件路径
 * @property string|null $is_external 是否外链（1是 0否）
 * @property string|null $is_cache 是否缓存（1缓存 0不缓存）
 * @property string|null $is_visible 是否显示（1显示 0不显示）
 * @property string|null $status 菜单状态（1正常 0禁用）
 * @property int|null $list_sort 显示顺序
 * @property string $remark 备注
 * @property int|null $create_time 创建时间
 * @property int|null $update_time 更新时间
 */
class Menu extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName(): string
    {
        return 'sys_menu';
    }

    /**
     * {@inheritdoc}
     */
    public function rules(): array
    {
        return [
            [['name', 'type'], 'required'],
            ['url', 'required', 'when' => function ($model) {
                return $model->type && $model->type != 'F';
            }],

            [['perms', 'icon', 'url', 'query', 'component', 'remark'], 'default', 'value' => ''],
            [['is_external'], 'default', 'value' => '0'],
            [['status', 'is_visible', 'is_cache'], 'default', 'value' => '1'],
            ['list_sort', 'default', 'value' => 10],
            ['parent_id', 'default', 'value' => 0],

            [['name', 'type', 'perms', 'icon', 'url', 'query', 'component', 'is_external', 'is_cache', 'is_visible', 'status', 'remark'], 'trim'],

            [['name','icon'], 'string', 'max' => 64],
            ['perms', 'string', 'max' => 128],
            [['url', 'query', 'component', 'remark'], 'string', 'max' => 250],
            [['is_external', 'is_cache', 'is_visible', 'status'], 'in', 'range' => ['0', '1']],
            [['type'], 'in', 'range' => ['M', 'C', 'F']],
            [['list_sort', 'parent_id'], 'integer'],

            [['create_time', 'update_time'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels(): array
    {
        return [
            'id' => '菜单ID',
            'name' => '菜单名称',
            'parent_id' => '上级菜单',
            'type' => '菜单类型',
            'perms' => '权限标识',
            'icon' => '菜单图标',
            'url' => '路由地址',
            'query' => '路由参数',
            'component' => '组件路径',
            'is_external' => '是否外链',
            'is_cache' => '是否缓存',
            'is_visible' => '是否显示',
            'status' => '菜单状态',
            'list_sort' => '显示顺序',
            'remark' => '备注',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function afterDelete()
    {
        if ($this->id) RoleMenu::deleteAll(['menu_id' => $this->id]);
        parent::afterDelete();
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
                ],
                'value' => function () {
                    return time();
                }
            ]
        ];
    }
}
